package diamondServer.client.parser;
import java.io.IOException;
import java.net.ContentHandler;
import java.net.InetSocketAddress;
import java.net.URLConnection;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

import diamondServer.clientserver.threadpool.ClientTask;
import diamondServer.clientserver.threadpool.PoolManager;
import diamondServer.clientserver.threadpool.Task;



public class ClientSideContentHandler implements org.xml.sax.ContentHandler {

	private int sendingTimes = 1;
	private long delay = 0;
	private long startTime = 0; 
	private String body = null;
	
	private PoolManager man;
	private InetSocketAddress adress;
	private int index = 0; // 1-sendingTimes 2-delay 3-StartTime 4-Body 
	
	
	public ClientSideContentHandler(InetSocketAddress inAdress) {
		this.adress = inAdress;
	}

	@Override
	public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
		
		String str = new String(arg0,arg1,arg2);
		
		if (index == 1) 
			{
			this.sendingTimes = Integer.parseInt(str);
			index = 0;
			}
		if (index == 2)
			{
			this.delay = Long.parseLong(str);
			index = 0;
			}
		if (index == 3)
			{
				this.startTime = Long.parseLong(str);
				index = 0;
			}
		if (index == 4) 
			{
				this.body = str;
				index = 0;
			}
	}

	@Override
	public void endDocument() throws SAXException {
//		System.out.println("ending parser the XML file!");
//		System.out.println(this.sendingTimes);
//		System.out.println(this.delay);
//		System.out.println(this.startTime);
//		System.out.println(this.body);
		try {
			man.InvokeAll();
			man.shutdown();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void endElement(String arg0, String arg1, String arg2)
			throws SAXException {
		
		if (arg1.equals("Message")) 
		{
			Task t = new ClientTask(this.body , this.adress ,this.sendingTimes,this.delay,this.startTime);
			man.addTask(t);
			
			  sendingTimes = 1;
			  delay = 0;
			  startTime = 0; 
			  body = null;
		}
		
		
	}

	@Override
	public void endPrefixMapping(String arg0) throws SAXException {
		
	}

	@Override
	public void ignorableWhitespace(char[] arg0, int arg1, int arg2)
			throws SAXException {
	}

	@Override
	public void processingInstruction(String arg0, String arg1)
			throws SAXException {
	//	System.out.println("IN processingInstruction "+ arg0 +","+arg1);
		
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setDocumentLocator(Locator arg0) {
		
	}

	@Override
	public void skippedEntity(String arg0) throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void startDocument() throws SAXException {
		System.out.println("start parsing the XML file!");
		man = new PoolManager();
		
	}

	@Override
	public void startElement(String arg0, String arg1, String arg2,
			Attributes arg3) throws SAXException {
		if (arg1.equals("SendingTimes")) index = 1;
		else if (arg1.equals("Delay")) index = 2;
		else if (arg1.equals("StartTime")) index = 3;
		else if (arg1.equals("Body")) index = 4;
	}

	@Override
	public void startPrefixMapping(String arg0, String arg1)
			throws SAXException {
		
		// TODO Auto-generated method stub
		
	}
	
}

